<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/7/2
 * Time: 16:24
 */
namespace app\api\controller;
use think\Db;
use think\Request;
class  Book extends Base {
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    /***
     * @return \think\response\Json
     * 轮播图
     */
    public function banner(){
        $banner = Db::table('up_banner1')
            ->where('banner_status', 1)
            ->field('banner_id, banner_name, banner_thumb, banner_type, banner_match_id, banner_link, 
            banner_content')
            ->order('banner_sort asc')
            ->limit(5)
            ->select();
        if($banner){
            return json(['status' => 200, 'msg' => $banner]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }

    public function bannerDetail(){
        extract(input());
        $banner = Db::table('up_banner1')
            ->where('banner_id',$banner_id)
            ->find();
        if($banner){
            return json(['status' => 200, 'msg' => $banner]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    /***
     * @return \think\response\Json
     * 分类
     */
    public function itemize(){
        $res = Db::table('up_book_itemize')->where('is_del',1)->field('book_itemize_id,book_itemize_name,book_itemize_url')->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //推荐书籍
    public function recommend(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['book_recommend'] =1;
        $where['book_status'] = 1;
        $res = Db::table('up_book')->where($where)
            ->field('book_id,book_name,book_img,book_brie,book_read')
            ->limit($limit,$num)
            ->order('book_read desc,book_add_time desc')
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //分类书籍
    public function index(){
        $page = input('param.page')?input('param.page'):1;
        $book_itemize_id = input('param.book_itemize_id');
        $num = 10;
        $limit = ($page-1)*$num;
        $where['book_itemize_id'] = $book_itemize_id;
        $where['book_status'] = 1;
        $res = Db::table('up_book')->where($where)
            ->field('book_id,book_name,book_img,book_brie,book_read')
            ->limit($limit,$num)
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //阅读量
    public function readNum(){
        $book_id = input('param.book_id');
        $res = Db::table("up_book")->where('book_id',$book_id)->setInc('book_read',1);
        if($res){
            return json(['status' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['status' => 200, 'msg' => "操作失败"]);
        }
    }
    //搜索
    public function keyword(){
        $page = input('param.page')?input('param.page'):1;
        $keyword = input('param.keyword');
        $num = 10;
        $limit = ($page-1)*$num;
        if($keyword==''){
            $where =array();
        }else{
            $where['book_name'] = array('like',"%$keyword%");
        }
        $res = Db::table('up_book')->where($where)
            ->field('book_id,book_name,book_img,book_brie,book_read')
            ->limit($limit,$num)
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    //书架
    public function bookSelf(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['book_user_id'] = $user_id;
        $res = Db::table('up_book_self')->alias('a')
            ->join('up_book ub','ub.book_id = a.book_id','left')
            ->limit($limit,$num)
            ->where($where)

            ->field('a.*,ub.book_name,ub.book_img')
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //加入书架
    public function addBookSelf(){
        extract(input());
        $book_id = input('param.book_id');
        $data['book_id'] = $book_id;
        $data['book_user_id'] = $user_id;
        $res = Db::table('up_book_self')->insertGetId($data);
        if($res){
            return json(['status' => 200, 'msg' => "加入成功"]);
        }else{
            return json(['status' => 200, 'msg' => "加入失败"]);
        }
    }
    //移除书架
    public function delBookSelf(){
        extract(input());
        $id = input('param.id');
        $where['book_self_id'] = array('IN',$id);
        $res = Db::table('up_book_self')->where($where)->delete();
        if($res){
            return json(['status' => 200, 'msg' => "移除成功"]);
        }else{
            return json(['status' => 200, 'msg' => "移除失败"]);
        }
    }
    //阅读页数
    public function readBook(){
        extract(input());
        $book_id = input('param.book_id');
        $where['book_id'] = $book_id;
        $where['user_id'] = $user_id;
        $info = Db::table('up_book_read')->where($where)->find();
        if($info['book_read_id']!=''){
           $res = Db::table('up_book_read')->where('book_read_id',$info['book_read_id'])->setInc('book_num',1);
        }else{
            $data['book_id'] = $book_id;
            $data['user_id'] = $user_id;
           $res =  Db::table('up_book_read')->insertGetId($data);
        }
        if($res){
            return json(['status' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['status' => 200, 'msg' => "操作失败"]);
        }
    }

    //书籍详情
    public function detailBook(){
        extract(input());
        $book_id = input('param.book_id');
        $res = Db::table('up_book')->where('book_id',$book_id)->find();
        $where['order_user_id'] = $user_id;
        $where['order_status'] = 2;
        $where['order_cate'] = array('IN',array(5,6));
        $where['order_book_id'] = $book_id;
        //查询改用户是否购买过书籍
        $info = Db::table('up_order')->where($where)->find();
        if($info['order_id']!=''){//证明该用户已经购买
            $res['is_buy'] = 1;
        }else{
            $res['is_buy'] = 2;
        }
        //查询目录
//        $info1 = Db::table('up_book_children')->where('book_id',$book_id)->select();
//        $res['children'] = $info1;
//        $res['num'] = count($info1);
        //查询是否加入书架
        $whereMap['book_id'] = $book_id;
        $whereMap['book_user_id'] = $user_id;
        $info2 = Db::table('up_book_self')->where($whereMap)->find();
        if($info2['book_self_id']!=''){
            $res['is_self'] = 1;
        }else{
            $res['is_self'] = 2;
        }

        return json(['status' => 200, 'msg' =>$res]);
    }

    //搜索书架
    public function keywordSelf(){
        extract(input(''));
        $keyword = input('param.keyword');
        $where['a.book_user_id'] = $user_id;
        if(isset($keyword) && $keyword != ''){
            $where['ub.book_name'] = array('like',"%$keyword%");
        }
        
        $res = Db::table('up_book_self')->alias('a')
            ->join('up_book ub','ub.book_id = a.book_id','left')
            ->where($where)
            ->field('a.*,ub.book_id,ub.book_name,ub.book_img')
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    //支付
    public function buyBook(){
        extract(input());
        $info = Db::table('up_u_user')->where('user_id',$user_id)->field('user_username,user_phone')->find();
        $price = input('param.price');
        $book_id = input('param.book_id');
        $order_number = date('YmdHis') . str_pad(mt_rand(1, 99999999), 6, '0', STR_PAD_LEFT).$user_id;
        $data['order_user_id'] = $user_id;
        $data['order_num'] = $order_number;
        $data['order_price'] = $price;
        $data['order_time'] = date('Y-m-d H:i:s');
        if($order_type == 1){ // 微信
            $data['order_type'] = 1;
        }else{
            $data['order_type'] = 2;
        }
        $data['order_cate'] = 5;
        $data['order_book_id'] = $book_id;
        $data['order_nickname'] = $info['user_username'];
        $data['order_phone'] = $info['user_phone'];
        $res = Db::table('up_order')->insertGetId($data);
        if($res){
            return json(['status' => 200, 'msg' => $res]);
        }else{
            return json(['status' => 200, 'msg' => "失败"]);
        }
    }
    
    /**
     * 判断用户有没有购买过书籍&&剩余免费阅读时间
     * @return \think\response\Json
     * book_id    书籍ID
     */
    public function readTime(){
        $book_id = input('book_id');
    
        // 查询该用户是否购买过书籍
        $where['order_user_id'] = $this->user_id;
        $where['order_status'] = 2;
        $where['order_cate'] = array('IN', array(5,6));
        $where['order_book_id'] = $book_id;
        $info = Db::table('up_order')->where($where)->find();
        if($info){ //证明该用户已经购买
            $is_buy = 1;
        }else{
            $is_buy = 2;
        }
        
        // 用户阅读时间
        $is_read = Db::table('up_book_read_time')
            ->where('bu_user_id', $this->user_id)
            ->find();
        if($is_read){
            $residue = $is_read['bu_time'];
        }else{
            Db::table('up_book_read_time')->insert(['bu_user_id' => $this->user_id]);
            $residue = 600;
        }
        return json(['status' => 200, 'msg' => '', 'is_buy' => $is_buy, 'residue' => $residue]);
    }
    
    /**
     * 扣除本次阅读时长
     * @return \think\response\Json
     * read_time     本次持续阅读时长（秒）
     */
    public function decReadTime(){
        $read_time = input('read_time'); // 此次阅读时长（秒）
        $time = Db::table('up_book_read_time')->where('bu_user_id', $this->user_id)->find();
        if($time['bu_time'] <= $read_time){
            $residue = 0;
        }else{
            $residue = $time['bu_time'] - $read_time;
        }
        Db::table('up_book_read_time')->where('bu_user_id', $this->user_id)->update(['bu_time' => $residue]);
        return json(['status' => 200, 'msg' => true]);
    }
}